package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Standardtype;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("StandardtypeDAO")
@Transactional
public class StandardtypeDAOImpl extends AbstractJpaDao<Standardtype> implements StandardtypeDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Standardtype.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public StandardtypeDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Standardtype findStandardtypeById(Integer id) throws DataAccessException {

		return findStandardtypeById(id, -1, -1);
	}

	@Transactional
	public Standardtype findStandardtypeById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardtypeById", startResult, maxRows, id);
			return (Standardtype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Standardtype> findStandardtypeByName(String name) throws DataAccessException {

		return findStandardtypeByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardtype> findStandardtypeByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardtypeByName", startResult, maxRows, name);
		return new LinkedHashSet<Standardtype>(query.getResultList());
	}

	@Transactional
	public Set<Standardtype> findStandardtypeByNameContaining(String name) throws DataAccessException {

		return findStandardtypeByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardtype> findStandardtypeByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardtypeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Standardtype>(query.getResultList());
	}

	@Transactional
	public Set<Standardtype> findStandardtypeByDescriptioin(String descriptioin) throws DataAccessException {

		return findStandardtypeByDescriptioin(descriptioin, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardtype> findStandardtypeByDescriptioin(String descriptioin, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardtypeByDescriptioin", startResult, maxRows, descriptioin);
		return new LinkedHashSet<Standardtype>(query.getResultList());
	}

	@Transactional
	public Set<Standardtype> findStandardtypeByDescriptioinContaining(String descriptioin) throws DataAccessException {

		return findStandardtypeByDescriptioinContaining(descriptioin, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardtype> findStandardtypeByDescriptioinContaining(String descriptioin, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findStandardtypeByDescriptioinContaining", startResult, maxRows, descriptioin);
		return new LinkedHashSet<Standardtype>(query.getResultList());
	}

	@Transactional
	public Standardtype findStandardtypeByPrimaryKey(Integer id) throws DataAccessException {
		return findStandardtypeByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Standardtype findStandardtypeByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardtypeByPrimaryKey", startResult, maxRows, id);
			return (Standardtype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Standardtype> findStandardtypesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardtypesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Standardtype> findAllStandardtypes() throws DataAccessException {

		return findAllStandardtypes(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardtype> findAllStandardtypes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllStandardtypes", startResult, maxRows);
		return new LinkedHashSet<Standardtype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Standardtype> findStandardtypeByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Standardtype>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Standardtype> findStandardtypesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Standardtype.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Standardtype> standardtypes = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return standardtypes;
	}

	@Transactional
	public Standardtype findStandardtypeByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findStandardtypeByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Standardtype findStandardtypeByPhysicalPrimaryKey(String name, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findStandardtypeByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Standardtype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Standardtype entity) {
		return true;
	}
}
